package com.xy.smartsms.plugincmcc;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;

import org.json.JSONObject;

/**
 * Created by Administrator on 2017/3/3.
 */

public class CMCCDataDBHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "cmcc_sdk.db";
    private static final int VERSION = 2;
    public static final String TABLE_NAME = "tb_cmcc_cache";

    public static final String SQL_DROP_TABLE = " DROP TABLE IF EXISTS " + TABLE_NAME;
    private static final String SQL_CREATE_TB = "CREATE TABLE  IF NOT EXISTS " + TABLE_NAME + " ("
            + "id INTEGER PRIMARY KEY,"
            + "msgId TEXT,"
            + "phoneNum TEXT,"
            + "smsMsg TEXT,"
            + "queryKey TEXT,"
            + "parseTime LONG,"
            + "result TEXT"
            + ")";

    public CMCCDataDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
                            int version) {
        super(context, name, factory, version);
    }

    public CMCCDataDBHelper(Context context, String name, int version) {
        this(context, name, null, version);
    }

    public CMCCDataDBHelper(Context context) {
        this(context, DB_NAME, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(SQL_CREATE_TB);
        sqLiteDatabase.execSQL(CMCCBeforeInfo.SQL_CREATE_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL(SQL_DROP_TABLE);
        sqLiteDatabase.execSQL(CMCCBeforeInfo.SQL_DROP_TABLE);
        sqLiteDatabase.execSQL(SQL_CREATE_TB);
        sqLiteDatabase.execSQL(CMCCBeforeInfo.SQL_CREATE_TABLE);

    }

    public static JSONObject loadSingleDataFromCursor(String[] columns, Cursor cursor) {
        if (cursor != null) {
            try {
                JSONObject tempObj = null;
                if (cursor.moveToNext()) {
                    tempObj = new JSONObject();
                    for (int i = 0; i < columns.length; i++) {
                        tempObj.put(columns[i], cursor.getString(i));
                    }
                }
                return tempObj;
            } catch (Throwable e) {

            }
        }
        return null;
    }

    /**
     * »ñÈ¡ ContentValuesÊý¾Ý
     *
     * @param values
     * @param args
     * @return
     */
    public static ContentValues getContentValues(ContentValues values, String... args) {
        return getContentValues(values, false, args);
    }

    /**
     * @param values
     * @param needValue key,Óëvalue±ØÐë¶¼ÓÐÖµ
     * @param args      key-value¶Ô
     * @return
     */
    public static ContentValues getContentValues(ContentValues values, boolean needValue, String... args) {
        if (args == null || args.length % 2 != 0)
            return null;
        if (values == null) {
            values = new ContentValues();
        }
        int len = args.length;
        for (int i = 0; i < len; i += 2) {
            if (needValue && (TextUtils.isEmpty(args[i]) || TextUtils.isEmpty(args[i + 1]))) {
                continue;
            }
            values.put(args[i], args[i + 1]);
        }
        return values;
    }
}
